<?php

require_once( "TestHarness.php" );


class PhpFaces_Test_Component_ComponentsTest extends PhpFaces_Test_AbstractPhpFacesTestCase {
	
	public function getConfigPath() {
		return PhpFaces_TestDir."configs/faces-config-component-test.xml";
	}
	
	public function getViewId() {
		return "views/hardcore/01.php";
	}
	
	public function testViewHandler() {
		$viewHandler = $this->facesContext->getApplication()->getViewHandler();

		$lifecycleFactory = PhpFaces_LifecycleFactory::GetInstance();
		$lifecycle = $lifecycleFactory->getLifecycle( "default" );
		
		$config  = $lifecycle->getConfig();
		//print_r( $config );


		$this->assertNotNull( $viewHandler );
		$this->assertTrue( $viewHandler instanceof PhpFaces_ViewHandler_HardCore );
		
		//print_r( $viewHandler );
		
	}
	
	public function testViewRoot() {
		$this->lifecycle->execute( $this->facesContext );
		
		$viewRoot = $this->facesContext->getViewRoot();
		$this->assertNotNull( $viewRoot );
		$this->assertNotNull( $viewRoot instanceof PhpFaces_Component_UIViewRoot );
	}
	
	public function testGetChildren() {
		$this->lifecycle->execute( $this->facesContext );
		
		$viewRoot = $this->facesContext->getViewRoot();
		
		$this->assertNotNull( $viewRoot->getChildren() );
		$this->assertEquals( 1, $viewRoot->getChildren()->getSize() );
	}
	
	public function testFindComponentDirectChild() {
		$this->lifecycle->execute( $this->facesContext );
		
		$viewRoot = $this->facesContext->getViewRoot();

		$component = $viewRoot->findComponent( "hello" );
		$this->assertNotNull( $component );
		$this->assertTrue( $component instanceof PhpFaces_Component_UIComponent );
		$this->assertEquals( "hello", $component->getId() );
	}
	
	public function testFindAbsoluteChild() {
		$this->lifecycle->execute( $this->facesContext );
		
		$viewRoot = $this->facesContext->getViewRoot();

		$component = $viewRoot->findComponent( ":hello" );
		$this->assertNotNull( $component );
		$this->assertTrue( $component instanceof PhpFaces_Component_UIComponent );
		$this->assertEquals( "hello", $component->getId() );
		
	}
	
	public function testRenderComponent() {

		$facesContext = $this->facesContext;
		
		$this->loadView( $this->getViewId() );
		$viewRoot = $facesContext->getViewRoot();
		
		$hello = $viewRoot->findComponent( "hello" );

		$writer = new XMLWriter();
		$writer->openMemory();
		$facesContext->setResponseWriter( $writer );
		
		$writer->startDocument();

		$hello->encodeBegin( $facesContext );
		$hello->encodeChildren( $facesContext );
		$hello->encodeEnd( $facesContext );
		
		$writer->endDocument();
		
		$output = $this->normalize( $writer->outputMemory( true ) );

		$expectedOutput = $this->normalize( '<?xml version="1.0"?><div xmlns="http://www.w3.org/1999/xhtml" id="hello">hello, phpfaces!</div>' );

		$this->assertEquals( $expectedOutput, $output );	
	}
	
	
	public function testRenderHtmlInput() {

		$facesContext = $this->facesContext;
		
		$this->loadView( "views/hardcore/02.php" );
		$viewRoot = $facesContext->getViewRoot();
		
		$hello = $viewRoot->findComponent( "hello" );

		$writer = new XMLWriter();
		$writer->openMemory();
		$facesContext->setResponseWriter( $writer );
		
		$writer->startDocument();

		$hello->encodeBegin( $facesContext );
		$hello->encodeChildren( $facesContext );
		$hello->encodeEnd( $facesContext );
		
		$writer->endDocument();
		
		$output = $this->normalize( $writer->outputMemory( true ) );

		$expectedOutput = $this->normalize( '<?xml version="1.0"?><input xmlns="http://www.w3.org/1999/xhtml" id="hello" value="hello, phpfaces!"/>' );

		$this->assertEquals( $expectedOutput, $output );	
	}
	
	public function testRenderHtmlForm() {

		$facesContext = $this->facesContext;
		
		$this->loadView( "views/hardcore/03.php" );
		$viewRoot = $facesContext->getViewRoot();
		
		$form = $viewRoot->findComponent( "form1" );
		
		
		

		$writer = new XMLWriter();
		$writer->openMemory();
		$facesContext->setResponseWriter( $writer );
		
		$writer->startDocument();
		
		PhpFaces_ComponentSupport::encodeRecursive( $facesContext, $form );
		//$form->encodeBegin( $facesContext );
		//$form->encodeChildren( $facesContext );
		//$form->encodeEnd( $facesContext );
		
		$writer->endDocument();
		
		$output = $this->normalize( $writer->outputMemory( true ) );

		$expectedOutput = $this->normalize( '<?xml version="1.0"?><form xmlns="http://www.w3.org/1999/xhtml" id="form1" action="#actionUrl"><input xmlns="http://www.w3.org/1999/xhtml" id="text1" value="hello, phpfaces 1!"/><input xmlns="http://www.w3.org/1999/xhtml" id="text2" value="hello, phpfaces 2!"/><input xmlns="http://www.w3.org/1999/xhtml" id="command1" type="submit" value="hello, phpfaces!"/></form>' );
		
		$this->assertEquals( $expectedOutput, $output );
	}
	
	public function testRenderPhase() {
		$this->lifecycle->execute( $this->facesContext );
		//ob_start();
		$this->lifecycle->render( $this->facesContext );
		//$output = ob_get_clean();
	
		//$this->assertNotNull( $output );
		
		$this->fail( "I need a way to test output." );	
	}
	
	
	protected function loadView( $viewId ) {
		$facesContext = $this->facesContext;
		
		$viewHandler = $facesContext->getApplication()->getViewHandler();
		$viewRoot = $viewHandler->createView( $facesContext, $viewId );
		
		$facesContext->setViewRoot( $viewRoot );
	}
	
	protected function normalize( $string ) {
		return str_replace( array( " ", "\n", "\r", "\t" ), "", $string );
	}
	
}
/*
 PhpFaces_Component_UIViewRoot Object
(
    [viewId:private] =>
    [idIncrement:private] => 1
    [renderKitId:private] => HTML_BASIC
    [events:private] =>
    [children:private] => PhpFaces_Lang_ListObject Object
        (
            [list:private] => Array
                (
                    [0] => PhpFaces_Component_UIOutput Object
                        (
                            [value:private] => hello, phpfaces!
                            [children:private] => PhpFaces_Lang_ListObject Object
                                (
                                    [list:private] => Array
                                        (
                                            [0] => PhpFaces_Component_UIOutput Object
                                                (
                                                    [value:private] => hello, phpfaces! CHILDO
                                                    [children:private] =>
                                                    [parent:private] => PhpFaces_Component_UIOutput Object
 *RECURSION*
                                                    [id:private] =>
                                                    [rendered:private] => 1
                                                    [rendererType:private] => javax.faces.Text
                                                )

                                        )

                                )

                            [parent:private] => PhpFaces_Component_UIViewRoot Object
 *RECURSION*
                            [id:private] =>
                            [rendered:private] => 1
                            [rendererType:private] => javax.faces.Text
                        )

                )

        )

    [parent:private] =>
    [id:private] =>
    [rendered:private] => 1
    [rendererType:private] =>
)

 */
?>